package name.matthewgreet.example4.action; import java.util.stream.Collectors; import javax.servlet.http.HttpSession; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Actions; import org.apache.struts2.convention.annotation.InterceptorRef; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; import name.matthewgreet.example4.action.FindGameListByDesignerNameAction.GameIdFinderByDesigner; import name.matthewgreet.example4.action.FindGameListByFamilyAction.GameListFinderByFamily; import name.matthewgreet.example4.action.FindGameListByNameContainsAction.GameListFinderByNameContains; import name.matthewgreet.example4.action.FindGameListByRankAction.GameListSizeFinderByRank; import name.matthewgreet.example4.common.AbstractViewActionSupport; import name.matthewgreet.example4.common.ListCache; import name.matthewgreet.example4.common.NA; import name.matthewgreet.example4.dto.GameDTO; import name.matthewgreet.example4.exception.ConfigurationException; import name.matthewgreet.example4.form.DesignerNameForm; import name.matthewgreet.example4.form.GameFamilyForm; import name.matthewgreet.example4.form.GameNameContainsForm; import name.matthewgreet.example4.form.GameRankForm; import name.matthewgreet.example4.model.DesignerTable; import name.matthewgreet.example4.model.GameTable; import name.matthewgreet.example4.model.Model; import name.matthewgreet.example4.util.GameCompositeCache; import name.matthewgreet.example4.view.GameListDisplay; /** * Displays search results of board games. */ @InterceptorRef("ViewStack") @Results({ @Result(name=ActionSupport.SUCCESS, location="/pages/GameList.jsp") }) public class ViewGameListAction extends AbstractViewActionSupport { public enum GameCategory {ALL, STRATEGY, ABSTRACT, FAMILY, THEMATIC, CUSTOMIZABLE, WAR, PARTY, CHILDREN} private static final long serialVersionUID = -7979376337555804889L; public static final int PAGE_SIZE = 6; private String refresh; private Integer pageNo; private GameNameContainsForm gameNameContainsForm; private GameRankForm gameRankForm; private DesignerNameForm designerNameForm; private GameFamilyForm gameFamilyForm; private GameListDisplay listDisplay; private String designersAutocompleteText; private String familiesAutocompleteText; private String makeDesignersAutocompleteText() throws ConfigurationException { DesignerTable designerTable; String result; // This is an obvious candidate for caching but that's not being demonstrated designerTable = Model.getInstance().getDesignerTable(); result = designerTable.findAll().stream().map(d -> d.getName().replace("\"", "\\\"")).sorted().collect(Collectors.joining("\",\n\"","\"", "\"")); return result; } private String makeFamiliesAutocompleteText() throws ConfigurationException { GameTable gameTable; String result; // This is an obvious candidate for caching but that's not being demonstrated gameTable = Model.getInstance().getGameTable(); result = gameTable.getFamilies().stream().map(d -> d.replace("\"", "\\\"")).sorted().collect(Collectors.joining("\",\n\"","\"", "\"")); return result; } @Actions({ @Action("/ViewGameList"), @Action("/") }) @Override public String execute() throws Exception { GameIdFinderByDesigner gameIdFinderByDesigner; GameListFinderByFamily gameListFinderByFamily; GameListFinderByNameContains gameListFinderByNameContains; GameListSizeFinderByRank gameListSizeFinderByRank; ListCache<NA,Integer,GameDTO> listCache; HttpSession session; GameCategory preferredCategory; session = getRequest().getSession(); listCache = GameCompositeCache.getInstance(session).getMasterListCache(); if (refresh != null) { listCache.forceReload(); } if (pageNo != null) { listCache.setPageNo(pageNo); } preferredCategory = GameCategory.ALL; if (gameNameContainsForm == null) { if (listCache.getBaseRecordListFinder() instanceof GameListFinderByNameContains) { gameListFinderByNameContains = (GameListFinderByNameContains)listCache.getBaseRecordListFinder(); gameNameContainsForm = new GameNameContainsForm(gameListFinderByNameContains.getNameContains()); } else { gameNameContainsForm = new GameNameContainsForm(); } } if (gameRankForm == null) { if (listCache.getListSizeFinder() instanceof GameListSizeFinderByRank) { gameListSizeFinderByRank = (GameListSizeFinderByRank)listCache.getListSizeFinder(); preferredCategory = gameListSizeFinderByRank.getCategory(); gameRankForm = new GameRankForm(gameListSizeFinderByRank.getCategory(), gameListSizeFinderByRank.getRankRangeStart(), gameListSizeFinderByRank.getRankRangeEnd()); } else { gameRankForm = new GameRankForm(); } } if (designerNameForm == null) { if (listCache.getIdListFinder() instanceof GameIdFinderByDesigner) { gameIdFinderByDesigner = (GameIdFinderByDesigner)listCache.getIdListFinder(); designerNameForm = new DesignerNameForm(gameIdFinderByDesigner.getDesignerName()); } else { designerNameForm = new DesignerNameForm(); } } if (gameFamilyForm == null) { if (listCache.getListFinder() instanceof GameListFinderByFamily) { gameListFinderByFamily = (GameListFinderByFamily)listCache.getListFinder(); gameFamilyForm = new GameFamilyForm(gameListFinderByFamily.getFamily()); } else { gameFamilyForm = new GameFamilyForm(); } } listDisplay = new GameListDisplay(listCache, preferredCategory); listDisplay.init(); designersAutocompleteText = makeDesignersAutocompleteText(); familiesAutocompleteText = makeFamiliesAutocompleteText(); return SUCCESS; } public String getRefresh() { return refresh; } public void setRefresh(String refresh) { this.refresh = refresh; } public Integer getPageNo() { return pageNo; } public void setPageNo(Integer pageNo) { this.pageNo = pageNo; } public GameNameContainsForm getGameNameContainsForm() { return gameNameContainsForm; } public GameRankForm getGameRankForm() { return gameRankForm; } public DesignerNameForm getDesignerNameForm() { return designerNameForm; } public GameFamilyForm getGameFamilyForm() { return gameFamilyForm; } public GameListDisplay getListDisplay() { return listDisplay; } public String getDesignersAutocompleteText() { return designersAutocompleteText; } public String getFamiliesAutocompleteText() { return familiesAutocompleteText; } }